package arm.dic.bean;

import arm.common.manager.TableManager;
import arm.db.tools.DBManager;
import arm.dic.entity.DicCategory;
import arm.dic.entity.DicMetricType;
import arm.dic.entity.DicNormi;
import arm.dic.entity.DicPeriod;

import javax.faces.model.SelectItem;
import java.util.ArrayList;
import java.util.List;

public class DicNormiBean
{

    private DicNormi dicNormi;
    private TableManager<DicNormi> tableManager;
    private DicPeriod selectedPeriod;
    private DicMetricType selectedMetricType;

    public DicNormi getDicNormi() {
        return dicNormi;
    }

    public void setDicNormi(DicNormi dicNormi) {
        this.dicNormi = dicNormi;
    }

    public String viewTable ()
    {
        getTableManager().readFromDB();
        return "viewDicNormiTable" ;
    }

    public TableManager<DicNormi> getTableManager () {
        if (tableManager==null)
        {
            tableManager=new TableManager<DicNormi>(DicNormi.class);
        }
        return tableManager;
    }

    public void setTableManager (TableManager<DicNormi> tableManager) {
        this.tableManager=tableManager;
    }

    public String create()
    {
        dicNormi = new DicNormi();
        return "editDicNormi";
    }

    public String save()
    {
        DBManager.saveOrUpdate(getDicNormi());
        return viewTable();
    }

    public String edit () {
        setDicNormi(getTableManager().getSelectedObj());
        return "editDicNormi";
    }

    public  String cancel () {
        return viewTable();
    }

    public void delete () {
        DBManager.delete(getTableManager().getSelectedObj());
        getTableManager().readFromDB();
    }

    public List<SelectItem> getPeriodList()
    {
        List<DicPeriod> objs = DBManager.readList(DicPeriod.class.getName());
        List<SelectItem> selectItems = new ArrayList();
        for (DicPeriod obj : objs)
        {
            selectItems.add(new SelectItem(obj, obj.getName()));
        }
        return selectItems;
    }

    public List<SelectItem> getMetricTypeList()
    {
        List<DicMetricType> objs = DBManager.readList(DicMetricType.class.getName());
        List<SelectItem> selectItems = new ArrayList();
        for (DicMetricType obj : objs)
        {
            selectItems.add(new SelectItem(obj, obj.getName()));
        }
        return selectItems;
    }

    public List<SelectItem> getCategoryList()
    {
        List<SelectItem> selectItems = new ArrayList();
        if (getDicNormi() != null && getDicNormi().getDicMetricType() != null){
            List<DicCategory> objs = DBManager.readList(DicCategory.class.getName()
                    + " as t WHERE t.dicMetricType.id="
                    + getDicNormi().getDicMetricType().getId());
            for (DicCategory obj : objs)
            {
                selectItems.add(new SelectItem(obj, obj.getName()));
            }
        }
        return selectItems;
    }



}